package com.creative.xcx.service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.creative.core.page.TableDataInfo;
import com.creative.core.uitls.Uidutil;
import com.creative.user.domain.UserInfo;
import com.creative.user.domain.dto.LoginDto;
import com.creative.user.service.UserInfoService;
import com.creative.xcx.domain.XcxPageData;
import com.creative.xcx.domain.vo.homeDataVo;
import com.creative.xcx.mapper.XcxPageDataMapper;
import com.creative.xcx.service.XcxPageDataService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ling
 * @description 小程序的页面数据(eg : Banner图片 ， 分类封面之类的)
 * @date 2025-01-15
 */
@Service
public class XcxPageDataServiceImpl implements XcxPageDataService {

    @Resource
    private XcxPageDataMapper xcxPageDataMapper;

    @Resource
    private UserInfoService userInfoService;


    @Override
    public int insert(XcxPageData xcxPageData) {
        System.out.println("xcxPageData = " + xcxPageData);

//        根据关键字段进行判断空操作
        if (xcxPageData.getCoverImg() == null && xcxPageData.getUrl() == null) {
            System.out.println("参数为空");
            return 0;
        }
//        获取用户信息
        UserInfo userInfo = userInfoService.selectById((String) StpUtil.getLoginId());

        xcxPageData.setCreateBy(userInfo.getNickName());
        //      随机生成uid (15位以内)
        String pdId = Uidutil.generateUid(15);
        xcxPageData.setPdId(pdId);
        xcxPageData.setUpdateBy(userInfo.getNickName());
        xcxPageData.setCreateTime(DateUtil.date());
        xcxPageData.setUpdateTime(DateUtil.date());

        return xcxPageDataMapper.insert(xcxPageData);
    }

    @Override
    public int delete(String pdId) {
        return xcxPageDataMapper.delete(pdId);
    }

    @Override
    public int update(XcxPageData xcxPageData) {
        xcxPageData.setUpdateTime(DateUtil.date());
        return xcxPageDataMapper.update(xcxPageData);
    }

    @Override
    public XcxPageData selectById(String pdId) {
//        System.out.println("pdId :" + pdId);
        return xcxPageDataMapper.selectById(pdId);
    }

    @Override
    public TableDataInfo selectXcxPageDataList(XcxPageData xcxPageData) {
        PageHelper.startPage(xcxPageData.getPageNum(), xcxPageData.getPageSize());
        List<XcxPageData> list = xcxPageDataMapper.selectXcxPageDataList(xcxPageData);
//      应该是在这进行排序等操作
        Page<XcxPageData> page = (Page<XcxPageData>) list;
        //    构建返回结果
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setCode(200);
        //      转换成VO输出
        List<homeDataVo> homeDataVo = BeanUtil.copyToList(page.getResult(), homeDataVo.class);
//        设置返回类型结果
        tableDataInfo.setRows(homeDataVo);
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    @Override
    public TableDataInfo getMPList(XcxPageData xcxPageData) {
        PageHelper.startPage(xcxPageData.getPageNum(), xcxPageData.getPageSize());
//        System.out.println("文件分页查询Order:" + xcxPageData);
        List<XcxPageData> list = xcxPageDataMapper.getMPList(xcxPageData);
//        转换为vo
        Page<XcxPageData> page = (Page<XcxPageData>) list;
        //    构建返回结果
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setCode(200);
//        设置返回类型结果
        tableDataInfo.setRows(page.getResult());
//        System.out.println("page.getResult() = " + page.getResult());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    //    通用模板 传入对象即可
    public TableDataInfo getInfo(XcxPageData xcxPageData) {
        PageHelper.startPage(xcxPageData.getPageNum(), xcxPageData.getPageSize());
        List<XcxPageData> list = xcxPageDataMapper.selectXcxPageDataList(xcxPageData);

        Page<XcxPageData> page = (Page<XcxPageData>) list;
        //    构建返回结果
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setCode(200);
//        设置返回类型结果
        tableDataInfo.setRows(page.getResult());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }
}

